package com.she.iiicoursecenter;

import java.util.List;

import com.she.iiicoursecenter.bean.SplashImageBean;
import com.she.iiicoursecenter.event.base.Event;
import com.she.iiicoursecenter.event.connect.bean.GetItemListBean;
import com.she.iiicoursecenter.event.connect.bean.GetMsgBean;
import com.she.iiicoursecenter.event.connect.bean.GetTokenBean;
import com.she.iiicoursecenter.event.connect.bean.SearchConditionedBean;
import com.she.iiicoursecenter.event.connect.bean.getMsgCountBean;
import com.she.iiicoursecenter.event.connect.event.GetItemListEvent;
import com.she.iiicoursecenter.event.connect.event.GetMsgEvent;
import com.she.iiicoursecenter.event.connect.event.GetTokenEvent;
import com.she.iiicoursecenter.event.connect.event.SearchConditionedEvent;
import com.she.iiicoursecenter.event.connect.event.SeverTimeEvent;
import com.she.iiicoursecenter.event.connect.event.getMsgCountEvent;
import com.she.iiicoursecenter.event.iface.IEventCallback;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.iface.IResourceBean;
import com.she.iiicoursecenter.iface.IResourceCallback;
import com.she.iiicoursecenter.manager.ResourceManager;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

public class Splash extends Activity {
	//
	private boolean mBreak = false;
	private boolean getDate = false;
	private boolean timeOut = false;
	//
	private ImageView mSplashImg;
	private SplashImageBean mSplashImageBean;
	private Bitmap mSplashBMP;
	//
	private int WHAT_JUMP_TO_MAIN = 0;
	private int WHAT_UPDATE_IMAGE = 1;
	// Jump to MainActivityGroup...
	private Handler JumpToMainHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			// super.handleMessage(msg);
			//
			if (msg.what == WHAT_JUMP_TO_MAIN) {
				if (mBreak == false) {
					Intent intent = new Intent(Splash.this, MainActivityGroup.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					Splash.this.startActivity(intent);
					Splash.this.finish();
				}
			} else if (msg.what == WHAT_UPDATE_IMAGE) {

				Splash.this.mSplashImg.invalidate();
			}
		}
	};

	public Splash() {
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//
		this.setContentView(R.layout.splash);
		//
		mSplashImg = (ImageView) this.findViewById(R.id.img_splash);
		mSplashImageBean = new SplashImageBean(null, 1, ((iiiApplication) this.getApplication()).mUtils.getScreenWidth(),
				((iiiApplication) this.getApplication()).mUtils.getScreenHeight());

		//
		ResourceManager.getInstance().asyncLoad(mSplashImageBean, new IResourceCallback() {

			public void onLoaded(Bitmap bitmap, IResourceBean bean) {
				// TODO Auto-generated method stub
				mSplashBMP = bitmap;
				mSplashImg.setImageBitmap(mSplashBMP);
				JumpToMainHandler.sendEmptyMessage(WHAT_UPDATE_IMAGE);
				//
			}

			public void onLoading(IResourceBean bean) {
				// TODO Auto-generated method stub

			}

		});

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//
		Thread t = new Thread(new Runnable() {

			public void run() {
				// TODO Auto-generated method stub
				try {
					//伺服器時間
					SeverTimeEvent severTimeEvent = new SeverTimeEvent(null);
					severTimeEvent.addCallback(new IEventCallback() {

						public void result(Event event, Object result) {
							if (KenEventManager.getInstance().getToken().equals("null")) {
								GetTokenEvent getTokenEvent = new GetTokenEvent(null);
								getTokenEvent.addCallback(new IEventCallback() {

									public void result(Event event, Object result) {
										if (((GetTokenBean) result).getResult() == 0) {
											KenEventManager.getInstance().setToken(((GetTokenBean) result).getToken());
											((iiiApplication)getApplicationContext()).setDEVICE_TOKEN(KenEventManager.getInstance().getToken());
										}
									}

									public void progress(Event event, int progress, String tmpPath) {
									}
								});
								KenEventManager.getInstance().execute(getTokenEvent);
							}else{
								((iiiApplication)getApplicationContext()).setDEVICE_TOKEN(KenEventManager.getInstance().getToken());
							}
						}
						public void progress(Event event, int progress, String tmpPath) {
						}
					});
					KenEventManager.getInstance().execute(severTimeEvent);
					//
					
//					//課程搜尋條件
//					SearchConditionedEvent searchConditionedEvent = new SearchConditionedEvent(null);
//					searchConditionedEvent.addCallback(new IEventCallback() {
//						
//						public void result(Event event, Object result) {
//							if (((SearchConditionedBean)result).getResult()==0) {
//								((iiiApplication)getApplicationContext()).setSearchConditionedData((SearchConditionedBean) result);
//							}
//						}
//						public void progress(Event event, int progress, String tmpPath) {
//						}
//					});
//					KenEventManager.getInstance().execute(searchConditionedEvent);
					//
					//新訊息數量
					getMsgCountEvent getMsgCountEvent = new getMsgCountEvent();
					getMsgCountEvent.addCallback(new IEventCallback() {
						
						public void result(Event event, Object result) {
							// TODO Auto-generated method stub
							((iiiApplication) Splash.this.getApplicationContext()).msgCount = ((getMsgCountBean)result).getCount();
							getDate = true;
							if (timeOut) {
								JumpToMainHandler.sendEmptyMessage(WHAT_JUMP_TO_MAIN);
							}
						}
						public void progress(Event event, int progress, String tmpPath) {
						}
					});
					KenEventManager.getInstance().execute(getMsgCountEvent);
					//
//					//訊息快遞
//					GetMsgEvent getMsgEvent = new GetMsgEvent(null);
//					getMsgEvent.addCallback(new IEventCallback() {
//						
//						public void result(Event event, Object result) {
//							if (((List<GetMsgBean>)result).size() >0) {
//								((iiiApplication) Splash.this.getApplication()).setMsgData((List<GetMsgBean>) result);
//								List<GetMsgBean> data = ((iiiApplication) Splash.this.getApplication()).getMsgData();
//								for (int i = 0; i < data.size(); i++) {
//									int msgid = ((iiiApplication) Splash.this.getApplication()).getMsgData().get(i).getMsgId();
//									long msgTime = ((iiiApplication) Splash.this.getApplication()).getMsgData().get(i).getTime();
//									if (KenEventManager.getInstance().getMsgIdRead(msgid) < msgTime) {
//										data.get(i).setIsNew(true);
//									}
//								}
//								getDate = true;
//								if (timeOut) {
//									JumpToMainHandler.sendEmptyMessage(WHAT_JUMP_TO_MAIN);
//								}
//							}else{
//								
//							}
//						}
//						public void progress(Event event, int progress, String tmpPath) {
//						}
//					});
//					KenEventManager.getInstance().execute(getMsgEvent);
//					//
					
//					//課程系列列表
//					GetItemListEvent getItemListEvent = new GetItemListEvent(null);
//					getItemListEvent.addCallback(new IEventCallback() {
//						public void result(Event event, Object result) {
//							if (((List<GetItemListBean>) result).size() > 0) {
//								((iiiApplication) Splash.this.getApplication()).setItemListBeanData((List<GetItemListBean>) result);
//								getDate = true;
//								if (timeOut) {
//									JumpToMainHandler.sendEmptyMessage(WHAT_JUMP_TO_MAIN);
//								}
//							}else{
//								
//							}
//						}
//
//						public void progress(Event event, int progress, String tmpPath) {
//						}
//					});
//					KenEventManager.getInstance().execute(getItemListEvent);
					//
					Thread.sleep(3000);
					timeOut = true;
					//
					if (getDate) {
						JumpToMainHandler.sendEmptyMessage(WHAT_JUMP_TO_MAIN);
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});
		t.start();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		//
		if (mSplashBMP == null) { // backup plan...
			mSplashImg.setImageResource(R.drawable.splash);
		}
	}

	@Override
	public void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		super.onDetachedFromWindow();
		//
		ResourceManager.getInstance().clearCache(mSplashImageBean.getSignature());
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		//
		mBreak = true;
		JumpToMainHandler.removeMessages(WHAT_JUMP_TO_MAIN);
		//
		super.onBackPressed();
		//
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}
